package cn.cloudtogo.server.module;

import cn.cloudtogo.common.model.ApiTokenDao;
import cn.cloudtogo.common.model.ApiTokenModel;
import cn.cloudtogo.common.model.ApiTokenRowMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.cloudtogo.common.GendbGlobalConstant.SCHEMA_SYSTEM;
import static cn.cloudtogo.common.GendbGlobalConstant.TABLE_API_TOKEN;

/**
 * @author yaolianhua789@gmail.com
 **/
@Slf4j
@Service
public class GendbApiTokenDao implements ApiTokenDao {

    private final JdbcTemplate jdbcTemplate;

    public GendbApiTokenDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<ApiTokenModel> list(String uuid) {
        String query = String.format("SELECT * FROM %s.%s WHERE _uuid = ? ", SCHEMA_SYSTEM, TABLE_API_TOKEN);
        return jdbcTemplate.query(query, new ApiTokenRowMapper(), uuid);
    }

    @Override
    public ApiTokenModel query(String uuid, String notes) {
        String query = String.format("SELECT * FROM %s.%s WHERE _uuid = ? and notes = ?", SCHEMA_SYSTEM, TABLE_API_TOKEN);
        try {
            ApiTokenModel apiTokenModel = jdbcTemplate.queryForObject(query, new ApiTokenRowMapper(), uuid, notes);
            return apiTokenModel;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public void delete(String uuid, String notes) {
        String deleteSql = String.format("DELETE FROM %s.%s WHERE _uuid = ? and notes = ?", SCHEMA_SYSTEM, TABLE_API_TOKEN);
        int update = jdbcTemplate.update(deleteSql, uuid, notes);
        log.info("Delete api token success. {}", update);
    }

    @Override
    public void deletes(String uuid) {
        String deleteSql = String.format("DELETE FROM %s.%s WHERE _uuid = ? ", SCHEMA_SYSTEM, TABLE_API_TOKEN);
        int update = jdbcTemplate.update(deleteSql, uuid);
        log.info("Delete api token list success. {}", update);
    }

    @Override
    public void insert(ApiTokenModel model) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("_uuid", model.getUuid());
        param.put("notes", model.getNotes());
        param.put("disable", model.isDisable());
        param.put("type", model.getType());
        param.put("_json", model.getJson());
        param.put("token", model.getToken());
        param.put("created_at", LocalDateTime.now());

        new SimpleJdbcInsert(jdbcTemplate)
                .withTableName(String.format("%s.%s", SCHEMA_SYSTEM, TABLE_API_TOKEN))
                .execute(param);
    }

    @Override
    public void update(String uuid, String notes, boolean disable) {
        String switchSql = String.format("update %s.%s set disable = ? where _uuid = ? and notes = ?", SCHEMA_SYSTEM, TABLE_API_TOKEN);
        int d = disable ? 1 : 0;
        jdbcTemplate.update(switchSql, d, uuid, notes);
    }
}
